/*
 * @Author: jinye.zhang
 * @Version: 1.0.0
 * @LastEditors: jinye.zhang
 * @Description: new file
 */
import config from './config'
import Vue from 'vue'
/**
 * @url {string} api地址 请求类型
 * @data {object} 请求参数
 * @method {string} 请求类型
 */
function request(params) {
	// console.log(Vue.prototype.$store.state.token)
	let businessId = '';
	// #ifdef MP
	businessId = config.id;
	// #endif
	// #ifdef H5
	businessId = uni.getStorageSync('businessId');
	// #endif
	return new Promise((resolve, reject) =>{
		const requestTask = uni.request({
			url: `${config.baseApi + params.url}`, // 接口地址
			method: params.method, // 请求类型
			data: params.data,
			timeout: 120 * 1000,
			header: {
        'businessId': businessId,
				'content-type': 'application/json', // 默认值
				'Authorization': 'Bearer ' + uni.getStorageSync('token'), // token
			},
			success: (res) => {
				if (res.statusCode === 200 || res.statusCode === 201) {
					if (res && res.data && res.data.code === 50014) {
						
						uni.reLaunch({
							url: '/pages/mine/login'
						})
					} else {
						resolve(res.data);
					}
				} else {
					if (res && res.data && res.data.code === 50014) {
						requestTask.abort();
						uni.showModal({
							title: '温馨提示',
							confirmColor: '#2979ff',
							content: '登录已过期，是否重新登录？',
							showCancel: false,
							success: function (res) {
								if (res.confirm) {
									// 重载
									uni.reLaunch({
										url: '/pages/mine/login'
									})
								} else if (res.cancel) {
										console.log('用户点击取消');
								}
							}
						});
					} else {
						requestTask.abort();
						uni.showToast({
							title: res.data.msg,
							icon: "none"
						})
					}
          reject();
				}
			},
			fail: () => {
				reject();
			}
		})
	})
}
	
export default request;